<?php
/*
$p=new FeedParser;
$p->setData(file_get_contents('/Users/surfchen/rss.xml'));
var_dump($p->getItems());
*/

class FeedParser {
    const FEED_TYPE_RSS2=1;
    const FEED_TYPE_ATOM=2;
    private $data;
    public function setData($data) {
        $this->data=$data;
    }
    /*
    each item with this data struct
    {
        title=>
        content=>content of html format
        pubdate=>unix time stamp
        author=>author
        link=>http url
    }
    */
    public function getItems() {
        $feed_items=array();
        $xml = new DOMDocument;
        $xml->loadXML($this->data);

        $items=$xml->getElementsByTagName('item');
        if ($items->length<=0) {
            $type=self::FEED_TYPE_ATOM;
            $items=$xml->getElementsByTagName('entry');
        } else {
            $type=self::FEED_TYPE_RSS2;
        }
        $results=array();
        foreach ($items as $item) {
            //优先权从左到右
            $content_keys=array('content:encoded','content','description');
            $content='';
            
            $pubdate_keys=array('pubDate','published');
            $pubdate=null;

            $author='';
            $title='';
            $link='';

            foreach ($item->childNodes as $node) {
                $see_one=false;
                foreach ($content_keys as $k=>$v) {
                    if ($node->nodeName==$v) {
                        $content=$node->nodeValue;
                        $see_one=true;
                    }
                    if ($see_one) {
                        unset($content_keys[$k]);
                    }
                }

                foreach ($pubdate_keys as $k=>$v) {
                    if ($node->nodeName==$v) {
                        $pubdate=$node->nodeValue;
                        break;
                    }
                }
            
                if ($node->nodeName=='dc:creator') {
                    $author=$node->nodeValue;
                } else if ($node->nodeName=='author') {
                    $_tmp_items=$node->getElementsByTagName('name');
                    $author=$_tmp_items->item(0)->nodeValue;
                } else if ($node->nodeName=='title') {
                    $title=$node->nodeValue;
                } else if ($node->nodeName=='link') {
                    if ($type==self::FEED_TYPE_ATOM) {
                        $link=$node->getAttribute('href');
                    } else {
                        $link=$node->nodeValue;
                    }
                }
                //var_dump($node->nodeName);
                //var_dump($node->nodeValue);
            }
            if (empty($content)) {
                continue;
            }
            if (!empty($pubdate)) {
                $pubdate=strtotime($pubdate);
            } else {
                $pubdate=time();
            }

            $feed_items[]=array(
                'title'=>$title,
                'author'=>$author,
                'pubdate'=>$pubdate,
                'link'=>$link,
                'content'=>$content,
            );
            /*
            echo "-------------\n";
            echo $title,"\n";
            echo $author,"\n";
            echo date('Y-m-d H:i',$pubdate),"\n";
            echo $link,"\n";
            //echo $content,"\n";
            */
        }
        return $feed_items;
    }
    public function getTitle() {
        $xml = new DOMDocument;
        $xml->loadXML($this->data);
        $items=$xml->getElementsByTagName('title');
        if ($items->length<=0) {
            return false;
        }
        $title=$items->item(0);
        return $title->firstChild->nodeValue;
    }
}
